<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/11
 * Time: 11:29
 */

namespace App\Http\Model;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WorkmanAccountsModel
{
    const TABLE = 'workman_accounts';

    /**
     * @param array $data
     * @return mixed
     */
    public function getAccountsList(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function($query)use ($data){
                if (!empty($data['employeeId'])){
                    $query->where('employeeId',$data['employeeId']);
                }
                if (!empty($data['startTime'])){
                    $query->where('separateTime','>=',$data['startTime']);
                }
                if (!empty($data['endTime'])){
                    $query->where('separateTime','<=',$data['endTime']);
                }
            })
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getAccountsListWithInfo(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('admin as a','a.id','=',self::TABLE.'.adminId')
            ->leftJoin('project as p','p.id','=',self::TABLE.'.projectId')
            ->leftJoin('project_area as pa','pa.id','=',self::TABLE.'.areaId')
            ->leftJoin('project_section as ps','ps.id','=',self::TABLE.'.sectionId')
            ->where(function($query)use ($data){
                if (!empty($data['employeeId'])){
                    $query->where('employeeId',$data['employeeId']);
                }
                if (!empty($data['startTime'])){
                    $query->where('separateTime','>=',$data['startTime']);
                }
                if (!empty($data['endTime'])){
                    $query->where('separateTime','<=',$data['endTime']);
                }
            })
            ->select('account','separateTime','batch',self::TABLE.'.remark','a.name as adminName','p.name as projectName','pa.name as areaName','ps.name as sectionName')
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)
            ->where(function($query)use($data){
                if (!empty($data['id'])){
                    $query->where(self::TABLE.'.id',$data['id']);
                }
                if (!empty($data['projectId'])){
                    $query->where(self::TABLE.'.projectId',$data['projectId']);
                }
                if (!empty($data['sectionId'])){
                    $query->where(self::TABLE.'.sectionId',$data['sectionId']);
                }
                if (!empty($data['groupId'])){
                    $query->where(self::TABLE.'.groupId',$data['groupId']);
                }
                if (!empty($data['employeeId'])){
                    $query->where(self::TABLE.'.employeeId',$data['employeeId']);
                }
                if (!empty($data['startTime']) && !empty($data['endTime'])){
                    $query->whereBetween('separateTime',[$data['startTime'],$data['endTime']]);
                }
                if (!empty($data['batch'])){
                    $query->where('batch',$data['batch']);
                }
            })
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }



    /**
     * @param $data
     * @return mixed
     */
    public function insert($data)
    {
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * @param $data
     * @param $where
     */
    public function update($data,$where)
    {
        DB::table(self::TABLE)->where($where)->update($data);
    }

    public function updateStatus(array $where,array $data){
        return DB::table(self::TABLE)
            ->where(function($query)use($where){
                if (!empty($where['id'])){
                    $query->where('id',$where['id']);
                }
                if (!empty($where['employeeId'])){
                    $query->where('employeeId',$where['employeeId']);
                }
                if (!empty($where['professionId'])){
                    $query->where('professionId',$where['professionId']);
                }
                if (!empty($where['sectionId'])){
                    $query->where('sectionId',$where['sectionId']);
                }
                if (!empty($where['areaId'])){
                    $query->where('areaId',$where['areaId']);
                }
                if (!empty($where['projectId'])){
                    $query->where('projectId',$where['projectId']);
                }
                if (!empty($where['startTime']) && !empty($where['endTime'])){
                    $query->whereBetween('separateTime',[$where['startTime'],$where['endTime']]);
                }
                if (!empty($where['status'])){
                    $query->where('status',$where['status']);
                }
                if (!empty($where['adminId'])){
                    $query->where('adminId',$where['adminId']);
                }
            })
            ->update($data);
    }

    /**
     * @param array $array
     * @return mixed
     */
    public function delete(array $array)
    {
        return DB::table(self::TABLE)->where($array)->delete();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getSum(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('employee',self::TABLE.'.employeeId','=','employee.id')
            ->where(function($query)use($data){
                if (!empty($data['projectId'])){
                    $query->where(self::TABLE.'.projectId',$data['projectId']);
                }
                if (!empty($data['professionId'])){
                    $query->where('employee.professionId',$data['professionId']);
                }
                if (!empty($data['areaId'])){
                    $query->where('areaId',$data['areaId']);
                }
                if (!empty($data['sectionId'])){
                    $query->where('sectionId',$data['sectionId']);
                }
                if (!empty($data['startTime'])){
                    $query->where('separateTime','>=',$data['startTime']);
                }
                if (!empty($data['endTime'])){
                    $query->where('separateTime','<=',$data['endTime']);
                }
                if (!empty($data['groupId'])){
                    $query->where(self::TABLE.'.groupId',$data['groupId']);
                }
                if (!empty($data['employeeId'])){
                    $query->where('employeeId',$data['employeeId']);
                }
                if (isset($data['status'])){
                    if (!is_array($data['status'])){
                        $query->where(self::TABLE.'.status',$data['status']);
                    }else{
                        $query->whereIn(self::TABLE.'.status',$data['status']);
                    }
                }
            })->sum('account');
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function lists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('employee as e','e.id','=',self::TABLE.'.employeeId')
            ->leftJoin('group_members as gm','gm.id','=',self::TABLE.'.memberId')
            ->leftJoin('admin as a','a.id','=',self::TABLE.'.adminId')
            ->where(self::TABLE.'.projectId',$data['projectId'])
            ->where('sectionId',$data['sectionId'])
            ->where(self::TABLE.'.groupId',$data['groupId'])
            ->select(self::TABLE.'.*','e.name as employeeName','e.jobNumber','gm.isLeader','a.name as adminName')
            ->get()->toArray();
    }

    public function export(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('employee as e','e.id','=',self::TABLE.'.employeeId')
            ->leftJoin('group_members as gm','gm.id','=',self::TABLE.'.memberId')
            ->leftJoin('admin as a','a.id','=',self::TABLE.'.adminId')
            ->where(self::TABLE.'.projectId',$data['projectId'])
            ->where('sectionId',$data['sectionId'])
            ->where(self::TABLE.'.groupId',$data['groupId'])
            ->where(function ($query) use ($data){
                if (isset($data['batch']) && $data['batch'] !== ''){
                    $query->where(self::TABLE.'.batch',$data['batch']);
                }
            })
            ->groupBy(self::TABLE.'.employeeId')
            ->select('e.name as employeeName','e.jobNumber','gm.isLeader','a.name as adminName',DB::raw('sum(account) as account'))
            ->get()->toArray();
    }
}